#include "CGame.h"

//#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000)? 1: 0)

CGame::CGame(void)
{
	
}

CGame::~CGame(void)
{
	this->GameEnd();
}

CGame::CGame(HINSTANCE hInstance, LPCSTR GameName, LPSTR lpCmdLine, int nShowCmd, int FPS, int ScreenWidth, int ScreenHeight)
{
	this->_hInstance = hInstance;
	this->_lpCmdLine = lpCmdLine;
	this->_nShowCmd = nShowCmd;
	this->_gameName = GameName;
	this->_FPS = FPS;
	 

	this->_screenWidth = ScreenWidth;
	this->_screenHeight = ScreenHeight;
}

LRESULT CALLBACK CGame::WinProc(HWND _hWnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_ACTIVATE:
		if ((int)WA_ACTIVE == wParam)
		{
			
		}

	}
	return DefWindowProc(_hWnd,msg,wParam,lParam);
}

int CGame::InitWindow()
{
	WNDCLASS 	wc;		

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)CGame::WinProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = _hInstance;
	wc.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
	wc.hCursor       = ::LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = (LPCWSTR)this->_gameName;

	if(!RegisterClass(&wc))
	{
		MessageBox(0, TEXT("RegisterClass - Failed"), 0, 0);
		return 0;
	}
	
	_hWnd = CreateWindow(wc.lpszClassName,
						wc.lpszClassName,
						WS_OVERLAPPEDWINDOW,
						//WS_EX_TOPMOST|WS_VISIBLE|WS_POPUP,
						CW_USEDEFAULT,
						CW_USEDEFAULT,
						800,
						600,
						NULL,
						NULL,
						_hInstance,
						0);
	if (_hWnd == 0)
	{
		MessageBox(NULL,L"Cannot create window!",L"Error",MB_OK);
		return 0;
	}

	ShowWindow(_hWnd,_nShowCmd);
	UpdateWindow(_hWnd);

	return 0;
}

int CGame::GameInit()
{
	this->InitWindow();

	//Init DirectX
	_directX = new DirectX();
	_directX->Init(_hWnd, _screenWidth, _screenHeight, false);

	//Init DirectInput
	/*_dInput = new DXinput(_hInstance,_hWnd);
	_dInput->InitDirectInput();
	_dInput->InitKeyboard();*/
	p_input = new DxInput();
	p_input->_InitDirectInput(_hInstance, _hWnd);

	this->LoadResource();
	return 1;
}

void CGame::GameLoop()
{
	MSG msg;
	long Tick = GetTickCount();

	DWORD tick_per_frame = 100 / _FPS;

	while (true)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			GameRender();
			DWORD now = GetTickCount();
			_DeltaTime = now - Tick;
			if (_DeltaTime > tick_per_frame)
			{
				
					GameUpdate();
					Tick = now;//GetTickCount();

			}

			//this->_dInput->PollKeyboard();
			p_input->_Process_KeyBoard();


			ProcessKeyboard();

		}
	}
}

void CGame::GameUpdate()
{
	
}

void CGame::GameRender()
{
	 
	//dev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);

	//LPDIRECT3DSURFACE9 backbuffer = _directX->getBackBuffer();

	if (_directX->GetDevice()->BeginScene())
	{
		//_directX->RenderSurface(_background);

		//_directX->getSpriteHandler()->Begin(D3DXSPRITE_ALPHABLEND);
		RenderFrame();
		//_directX->getSpriteHandler()->End();
		_directX->GetDevice()->EndScene();
	}

	_directX->GetDevice()->Present(NULL,NULL,NULL,NULL);
	/*if (KEY_DOWN(VK_ESCAPE))
		PostMessage(_hWnd,WM_DESTROY,0,0);*/
 
}

void CGame::GameEnd()
{
	delete _directX;
	//delete _dInput;
}

void CGame::ProcessKeyboard()
{
	/*for (int i=0;i<256;i++)
	{
		if (this->_dInput->isKeyPress(i))
			OnKeyDown(i);
		else OnKeyUp(i);
	}*/
}